package cn.stylefeng.guns.modular.customer.controller;

import cn.stylefeng.guns.base.pojo.page.LayuiPageInfo;
import cn.stylefeng.guns.modular.course.entity.Course;
import cn.stylefeng.guns.modular.customer.entity.Customer;

import cn.stylefeng.guns.modular.customer.service.CustomerService;
import cn.stylefeng.roses.core.base.controller.BaseController;
import cn.stylefeng.roses.kernel.model.response.ResponseData;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.List;


/**
 * 用户表控制器
 *
 * @author typ
 * @Date 2021-04-25 17:37:00
 */
@Controller
@RequestMapping("/customer")
public class CustomerController extends BaseController {

    private String PREFIX = "/customer";

    @Autowired
    private CustomerService customerService;

    /**
     * 跳转到主页面
     *
     * @author typ
     * @Date 2021-04-25
     */
    @RequestMapping("")
    public String index(Integer pid) {
        return PREFIX + "/customer.html";
    }

    /**
     * 跳转设置扶持领导弹窗页面
     *
     * @author typ
     * @Date 2021-04-25
     */
    @RequestMapping("leaders")
    public String leaders() {
        return PREFIX + "/leaders.html";
    }

    /**
     * 新增页面
     *
     * @author typ
     * @Date 2021-04-25
     */
    @RequestMapping("/add")
    public String add() {
        return PREFIX + "/customer_add.html";
    }

    /**
     * 编辑页面
     *
     * @author typ
     * @Date 2021-04-25
     */
    @RequestMapping("/edit")
    public String edit() {
        return PREFIX + "/customer_edit.html";
    }

    /**
     * 新增接口
     *
     * @author typ
     * @Date 2021-04-25
     */
    @RequestMapping("/addItem")
    @ResponseBody
    public ResponseData addItem(Customer customerParam) {
        this.customerService.add(customerParam);
        return ResponseData.success();
    }

    /**
     * 编辑接口
     *
     * @author typ
     * @Date 2021-04-25
     */
    @RequestMapping("/editItem")
    @ResponseBody
    public ResponseData editItem(Customer customerParam) {
        this.customerService.update(customerParam);
        return ResponseData.success();
    }

    /**
     * 删除接口
     *
     * @author typ
     * @Date 2021-04-25
     */
    @RequestMapping("/delete")
    @ResponseBody
    public ResponseData delete(Customer customerParam) {
        this.customerService.delete(customerParam);
        return ResponseData.success();
    }

    /**
     * 查看详情接口
     *
     * @author typ
     * @Date 2021-04-25
     */
    @RequestMapping("/detail")
    @ResponseBody
    public ResponseData detail(Customer customerParam) {
        Customer detail = this.customerService.getById(customerParam.getId());
        return ResponseData.success(detail);
    }

    /**
     * 查询列表
     *
     * @author typ
     * @Date 2021-04-25
     */
    @ResponseBody
    @RequestMapping("/list")
    public LayuiPageInfo list(Customer customerParam) {
        return this.customerService.findPageBySpec(customerParam);
    }

    /*
   修改社长和学院
    */
    @RequestMapping("/setHot")
    @ResponseBody
    public ResponseData setHot(int id,Integer isHot) {
        if(isHot==2) {
            Customer customers = new Customer();
            customers.setPid(id);
            List<Customer> customerList = customerService.customerLists(customers);
            if (customerList != null && customerList.size() > 0) {
                for (Customer customer : customerList) {
                    customer.setLeader(id);
                    customerService.update(customer);
                }
            }
        }
        Customer customer = this.customerService.getById(id);
        customer.setRole(isHot);
        customerService.update(customer);
        return ResponseData.success(customer);
    }

    /*
  修改社长和学院
   */
    @RequestMapping("/leader")
    @ResponseBody
    public ResponseData leader(int id,int leader) {
        Customer customer=new Customer();
        customer.setId(id);
        customer.setLeader(leader);
        customerService.update(customer);

        return ResponseData.success(customer);
    }

}


